package cn.karent.common;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 默认全局异常处理器
 *
 * @author wanshengdao
 * @date 2025/3/21
 */
@Slf4j
@ControllerAdvice
@RequiredArgsConstructor
public class DefaultExceptionHandler {

    @ResponseBody
    @ExceptionHandler(NoSuchBeanDefinitionException.class)
    public Result<String> handleNoSuchBeanDefinition(NoSuchBeanDefinitionException e) {
        log.info("异常: ", e);
        return Result.fail("400", "没有定义这个Bean");
    }

    @ResponseBody
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<String> handleIllegalArgumentException(IllegalArgumentException e) {
        log.info("业务异常: ", e);
        return Result.fail("400", e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(Throwable.class)
    public Result<String> handleThrowable(Throwable e) {
        log.error("异常: ", e);
        return Result.fail("500", "系统内部异常");
    }

}
